import { _decorator, Color, Component, director, find, instantiate, Node, Prefab, Sprite, SpriteFrame } from 'cc';
import { UIManager } from '../common/UIManager';
import { PrefabEnum } from '../common/PrefabEnum';
import { Village } from '../entity/Village';
import { BattleLayout } from './BattleLayout';
import { VillageCamera } from '../village/VillageCamera';
import { WorldCamera } from '../common/WorldCamera';
import { UIHeader } from '../common/UIHeader';
import { TabLayout } from '../common/TabLayout';
import { EventManager } from '../event/EventManager';
const { ccclass, property } = _decorator;



@ccclass('ArmyIndex')
export class ArmyIndex extends Component {

    @property(UIHeader)
    uiHeader:UIHeader
    @property(TabLayout)
    tabLayout:TabLayout

   
    @property(Node)
    public content:Node
    @property(Prefab)
    battleLayout:Prefab
    @property(Prefab)
    armyList:Prefab
    @property(Prefab)
    towerLayout:Prefab
    @property(SpriteFrame)
    btnSpriteFrame1:SpriteFrame
    @property(SpriteFrame)
    btnSpriteFrame2:SpriteFrame
    private haveContent:boolean = false

    start() {
       
        this.uiHeader.setData('出征')
        this.tabLayout.setData([ '军队管理', '出征'])
       
        EventManager.instance.on('军队管理', this.onArmy, this)
        EventManager.instance.on('出征', this.onBattle, this)
        EventManager.instance.on('出征close', this.onClose, this)

     
        
        if(!this.haveContent){
            this.onArmy()
        }
        
    }

    protected onDisable(): void {   
        // let sceneName = director.getScene().name
        // if(sceneName == 'village'){
        //     VillageCamera.instance.enabled = false
        // }
        // if(sceneName == 'world'){
        //     WorldCamera.instance.enabled = false
        // }
    }

    onTower(){
        // this.setActive(this.tower)
        // this.content.destroyAllChildren()
        // let node = instantiate(this.towerLayout)
        // find('Canvas').addChild(node)
        // UIManager.instance.closeUI(PrefabEnum.ArmyIndex)

        UIManager.instance.loadScene('tower')
    }
    onArmy(){
        find('Canvas/TowerLayout')?.destroy()
        
        this.scheduleOnce(() => {
            this.tabLayout.setActiveByName('军队管理')
        }, 0.5)
        this.content.destroyAllChildren()
        let node = instantiate(this.armyList)
        this.content.addChild(node)
    }
    onBattle(village?:Village){
        find('Canvas/TowerLayout')?.destroy()

        this.haveContent = true
        this.scheduleOnce(() => {
            this.tabLayout.setActiveByName('出征')
        }, 0.5)
        
        this.content.destroyAllChildren()
        let node = instantiate(this.battleLayout)
        this.content.addChild(node)
        if(village && village.x){
            node.getComponent(BattleLayout).onTargetVillage(village)
        }
    }
    onClose(){
        UIManager.instance.closeUI(PrefabEnum.ArmyIndex)
    }

   
}


